<template>
	<view class="mine-container" :style="{ height: `${windowHeight}px` }">
		<!--顶部个人信息栏-->
		<view class="header-section">
			<view class="section_icon">
				<view class="bell-icon-wrapper">
					<up-icon name="bell-fill" color="#333" size="25px" @click="goNews"></up-icon>
					<view v-if="hasTodo" class="badge-dot"></view>
				</view>
			</view>
			<view class="flex padding justify-between" v-if='token'>
				<view class="flex align-center">
					<image src="../../static/images/mine/cargoTran.png" class="cu-avatar round" mode="widthFix">
					</image>
					<view class="user-info">
						<view class="u_image">
							用户名
							<view class="">
								<image src="../../static/images/mine/LoginSucc.png" mode="widthFix"></image>
							</view>
						</view>
						<view class="u_title">
							{{ username }}
						</view>
					</view>
				</view>
			</view>
			<view class="flex padding align-center justify-between" v-else>
				<view class="flex align-center">
					<image src="../../static/images/mine/user.png" class="cu-avatar round" mode="widthFix">
					</image>
				</view>
				<view class="user-info" style="margin-right: 20px;" @click="goLogin">
					登录
				</view>
			</view>
		</view>
		<!-- 宫格组件 -->
		<uni-section title="个人信息" type="line"></uni-section>
		<view class="grid-body">
			<uni-grid :column="4" :showBorder="false">
				<uni-grid-item>
					<view class="grid-item-box" @click="goUserDetail">
						<image src="../../static/images/mine/infor.png"></image>
						<text class="text">个人信息</text>
					</view>
				</uni-grid-item>
				<uni-grid-item v-if="role === 'driver'">
					<view class="grid-item-box" @click="goCarDetail">
						<image src="../../static/images/mine/car.png"></image>
						<text class="text">车辆信息</text>
					</view>
				</uni-grid-item>
				<uni-grid-item>
					<view class="grid-item-box" @click="goBankCard">
						<image src="../../static/images/mine/payment.png"></image>
						<text class="text">收款信息</text>
					</view>
				</uni-grid-item>
				<uni-grid-item>
					<view class="grid-item-box" @click="goSigningCenter">
						<image src="../../static/images/mine/signup.png"></image>
						<text class="text">签约中心</text>
					</view>
				</uni-grid-item>
				<uni-grid-item>
					<view class="grid-item-box" @click="goMyWaybill">
						<image src="../../static/images/mine/waybill.png"></image>
						<text class="text">我的运单</text>
					</view>
				</uni-grid-item>
				<uni-grid-item>
					<view class="grid-item-box" @click="goEntrustedPayee">
						<image src="../../static/images/mine/commission.png"></image>
						<text class="text">委托收款人</text>
					</view>
				</uni-grid-item>
				<uni-grid-item>
					<view class="grid-item-box" @click="addClient">
						<image src="../../static/images/mine/add_client.png"></image>
						<text class="text">添加委托人</text>
					</view>
				</uni-grid-item>
				<uni-grid-item>
					<view class="grid-item-box" @click="goMyEnt">
						<image src="../../static/images/mine/ent.png"></image>
						<text class="text">承运企业</text>
					</view>
				</uni-grid-item>
				<uni-grid-item>
					<view class="grid-item-box" @click="goFeedback">
						<image src="../../static/images/mine/complaint.png"></image>
						<text class="text">投诉反馈</text>
					</view>
				</uni-grid-item>
				<uni-grid-item v-if="token">
					<view class="grid-item-box" @click="handleLogout">
						<image src="../../static/images/mine/exit.png"></image>
						<text class="text">退出</text>
					</view>
				</uni-grid-item>
			</uni-grid>
		</view>
	</view>
	<up-toast ref="uToastRef"></up-toast>
	<up-modal title="系统提示" :show="show" showCancelButton @confirm="confirm" @cancel="cancel">
		<view class="slot-content" style="text-align: center;">
			您确定退出系统吗
		</view>
	</up-modal>
</template>

<script lang="ts" setup>
import {
	useUserStore
} from '@/store/modules/user'
import {
	ref,
	nextTick,
	computed
} from "vue"
import {
	onShow, onLoad
} from "@dcloudio/uni-app"
import {
	judgeIdentify,
	getAssignor
} from "@/api/driver"
import {
	storeToRefs
} from "pinia"

/* hooks */
// 退出登录时清楚pinia和缓存的token，username
const {
	LOGOUT
} = useUserStore()
// 从pinia拿出token，根据token有无实现后续逻辑
const {
	token,
	username,
	role
} = storeToRefs(useUserStore())

/* 数据 */
const show = ref(false)
const windowHeight = computed(() => uni.getWindowInfo().windowHeight - 50)
// 提示
const uToastRef = ref<any>(null)
// 用户验证状态字段返回值 NOT_REAL代表未实名，DRIVER_REAL代表司机实名，CAPTAIN_REAL代表船长实名，ASSIGNOR_REAL代表委托人实名，实名了多个角色会以“,”连接起来
const userVerification = ref('')
// 有没有信息，小红点显示
const hasTodo = ref(false)


/* 函数 */
const goLogin = () => {
	uni.navigateTo({
		url: "/pages/selectRole"
	})
}
// token验证
const tokenVerification = () => {
	if (!token.value) {
		uToastRef.value.show({
			message: '您还未登录，请先登录',
			type: 'warning',
			duration: 3000
		})
		return false
	}
	return true
}
// 是否实名验证
const verification = () => {
	if (userVerification.value.includes('DRIVER_REAL') || userVerification.value.includes('ASSIGNOR_REAL') ||
		userVerification.value.includes('CAPTAIN_REAL')) {
		return true
	} else {
		return false
	}
}
// 消息中心
const goNews = () => {
	if (!tokenVerification()) return
	uni.navigateTo({
		url: "/package_mine/pages/news/index"
	})
}
// 用户信息
const goUserDetail = () => {
	if (!tokenVerification()) return
	if (verification()) {
		uni.navigateTo({
			url: "/package_mine/pages/auth/userInfo"
		})
	} else {
		if (role.value === 'captain') {
			uni.navigateTo({
				url: "/package_mine/pages/auth/captainAuth"
			})
		} else {
			uni.navigateTo({
				url: "/package_mine/pages/auth/driverAuth"
			})
		}
	}
}
// 车辆信息
const goCarDetail = () => {
	if (!tokenVerification()) return
	if (verification()) {
		uni.navigateTo({
			url: "/package_mine/pages/car/car"
		})
	} else {
		uni.navigateTo({
			url: "/package_mine/pages/auth/driverAuth"
		})
	}
}
// 收款信息
const goBankCard = () => {
	if (!tokenVerification()) return
	if (verification()) {
		uni.navigateTo({
			url: "/package_mine/pages/payReceive/index"
		})
	} else {
		if (role.value === 'captain') {
			uni.navigateTo({
				url: "/package_mine/pages/auth/captainAuth"
			})
		} else {
			uni.navigateTo({
				url: "/package_mine/pages/auth/driverAuth"
			})
		}
	}
}
// 签约中心
const goSigningCenter = () => {
	if (!tokenVerification()) return
	uni.navigateTo({
		url: "/package_mine/pages/signingCenter/index"
	})
}
// 我的运单
const goMyWaybill = () => {
	if (!tokenVerification()) return
	uni.navigateTo({
		url: "/package_mine/pages/waybill/index"
	})
}
// 委托收款人
const goEntrustedPayee = () => {
	if (!tokenVerification()) return
	if (userVerification.value.includes('ASSIGNOR_REAL')) {
		uToastRef.value.show({
			position: 'top',
			message: '您已认证委托收款人，两秒后跳转收款信息页面',
			type: 'success',
			complete() {
				uni.navigateTo({
					url: "/package_mine/pages/payReceive/index"
				})
			}
		})
	} else {
		uni.navigateTo({
			url: "/package_mine/pages/entrustedPayee/index"
		})
	}
}
// 添加委托人
const addClient = () => {
	if (!tokenVerification()) return
	if (userVerification.value.includes('ASSIGNOR_REAL')) {
		uni.navigateTo({
			url: "/package_mine/pages/entrustedPayee/assignorList"
		})
	} else {
		uToastRef.value.show({
			position: 'top',
			message: '您还未认证委托人',
			type: 'warning'
		})
	}
}
// 承运企业
const goMyEnt = () => {
	if (!tokenVerification()) return
	uni.navigateTo({
		url: "/package_mine/pages/ent/index"
	})
}
// 投诉反馈
const goFeedback = () => {
	if (!tokenVerification()) return
	uni.navigateTo({
		url: "/package_mine/pages/feedback/index"
	})
}
// 获取用户验证状态，NOT_REAL代表未实名，DRIVER_REAL代表司机实名，ASSIGNOR_REAL代表委托人实名，实名了多个角色会以“,”连接起来
const getUserVerification = async () => {
	uToastRef.value.show({
		message: '加载中...',
		type: 'loading',
		duration: -1
	})
	let res = await judgeIdentify()
	uToastRef.value.hide()
	if (res.success) {
		userVerification.value = res.data
	}
}
// 退出登录提示框
const handleLogout = async () => {
	show.value = true
}
// 确认
const confirm = () => {
	LOGOUT()
	show.value = false
	uToastRef.value.show({
		message: '您已退出登录',
		type: 'warning'
	})
}
// 取消
const cancel = () => {
	show.value = false
}
// 获取信息列表
const getTodoList = async () => {
	let res = await getAssignor({})
	if (res.success) {
		hasTodo.value = res.data.list && res.data.list.length > 0
	} else {
		hasTodo.value = false
	}
}
// 初始化封装
const init = async () => {
	// 因为下面请求中会调用uToastRef，onshow不能获取dom元素，所以需要nextTick
	await nextTick()
	if (token.value) {
		await getUserVerification()
		await getTodoList()
	}
}


/* 钩子 */
// tabar每次点击不会触发其他钩子，只能写在onShow里
onShow(() => {
	init()
})

// 页面级配置分享转发
// #ifdef MP-WEIXIN
onLoad(() => {
	uni.showShareMenu({
		withShareTicket: true, // 是否使用带 shareTicket 的转发
		menus: ['shareAppMessage', 'shareTimeline'] // 可分享到朋友圈
	})
})
// #endif
</script>

<style lang="scss" scoped>
page {
	display: flex;
	flex-direction: column;
	box-sizing: border-box;
	background-color: #fff;
	min-height: 100%;
	height: auto;
}

.bell-icon-wrapper {
	position: relative;
	display: inline-block;
	padding: 10px; // 增加点击区域

	.badge-dot {
		position: absolute;
		top: 2px;
		right: 2px;
		width: 8px;
		height: 8px;
		background-color: #FF2E2E;
		border-radius: 50%;
		border: 1px solid #fff; // 白色边框增加对比度
		z-index: 10;
		animation: pulse 1.5s infinite; // 添加呼吸动画效果
	}

	@keyframes pulse {
		0% {
			transform: scale(1);
			opacity: 1;
		}

		50% {
			transform: scale(1.2);
			opacity: 0.8;
		}

		100% {
			transform: scale(1);
			opacity: 1;
		}
	}
}

.mine-container {
	width: 100%;
	height: 100%;


	.header-section {
		padding: 45rpx 30rpx 45rpx 30rpx;
		background-color: #e3eeff;
		color: white;

		.section_icon {
			position: relative;
			float: right;
		}

		.login-tip {
			font-size: 18px;
			margin-left: 10px;
		}

		.cu-avatar {
			border: 2px solid #e3eeff;
			background-color: white;
			width: 100px;
			height: 100px;
			font-size: 2.5rm;

			.icon {
				font-size: 40px;
			}
		}

		.points {
			text-align: right;

			text {
				font-size: 15px;
				color: #1296DB;
				border-bottom: 1px solid #999;
			}
		}

		.user-info {
			margin-left: 15px;
			color: #1296DB;

			.u_image {
				display: flex;
				font-size: 17px;
				line-height: 30px;

				view {
					margin: 8rpx 0 0 10rpx;
				}

				image {
					width: 42rpx;
				}
			}
		}
	}

	.text {
		text-align: center;
		font-size: 26rpx;
		margin-top: 10rpx;
	}

	.grid-item-box {
		flex: 1;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding: 15px 0;
	}

	.grid-item-box image {
		width: 60rpx;
		height: 60rpx;
	}

	.uni-margin-wrap {
		width: 690rpx;
		width: 100%;
		;
	}

	.swiper {
		height: 300rpx;
	}

	.swiper-box {
		height: 150px;
	}

	.swiper-item {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: column;
		justify-content: center;
		align-items: center;
		color: #fff;
		height: 300rpx;
		line-height: 300rpx;
	}

	@media screen and (min-width: 500px) {
		.uni-swiper-dot-box {
			width: 400px;
			/* #ifndef APP-NVUE */
			margin: 0 auto;
			/* #endif */
			margin-top: 8px;
		}

		.image {
			width: 100%;
		}
	}
}
</style>